package com.zjhn.ds.boot;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjhn.ds.common.Constant;
import com.zjhn.ds.common.annotate.IgnoreTenant;
import com.zjhn.ds.domain.entity.erp.Department;
import com.zjhn.ds.domain.entity.erp.Operator;
import com.zjhn.ds.domain.entity.jlw.*;
import com.zjhn.ds.domain.vo.EquipmentVo;
import com.zjhn.ds.domain.vo.ModelVo;
import com.zjhn.ds.domain.vo.OperatorVo;
import com.zjhn.ds.domain.vo.StationVo;
import com.zjhn.ds.mapper.*;
import com.zjhn.ds.utils.base.RedisCache;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @BelongsProject: ds_jlw
 * @BelongsPackage: com.zjhn.ds.boot
 * @Author: sxt
 * @CreateTime: 2023-09-07  17:51
 * @Description:
 * @Version: 1.0
 */
@Component
@IgnoreTenant
public class CacheHandler {

    @Resource
    private RedisCache redisCache;

    @Resource
    private OperatorMapper operatorMapper;

    @Resource
    private StationMapper stationMapper;

    @Resource
    private EquipmentTypeMapper equipmentTypeMapper;

    @Resource
    private ModelMapper modelMapper;

    @Resource
    private WorkshopMapper workshopMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private EquipmentMapper equipmentMapper;

    @Resource
    private BindInfoMapper bindInfoMapper;

    /**
     * 缓存加载
     */
    public void cacheLoad() {

        // 缓存车间信息
        workshopLoad();

        // 缓存设备类别
        equipmentTypeLoad();

        // 缓存模型数据
        modelLoad();

        // 缓存员工信息
        operateLoad();

        // 缓存部门信息
        departmentLoad();

        // 缓存工位信息
        stationLoad();

        // 缓存设备信息
        equipmentLoad();

        // 缓存工位绑定设备信息
        bindInfoLoad();
    }

    /**
     * 缓存车间信息
     */
    private void workshopLoad() {
        List<Workshop> workshops = workshopMapper.selectList(new QueryWrapper<>());
        for (Workshop workshop : workshops)
            redisCache.setCacheObject(Constant.REDIS_PRE.WORKSHOP + workshop.getId(), workshop.getName());
    }

    /**
     * 缓存设备类别
     */
    private void equipmentTypeLoad() {
        List<EquipmentType> equipmentTypes = equipmentTypeMapper.selectList(new QueryWrapper<>());
        for (EquipmentType equipmentType : equipmentTypes)
            redisCache.setCacheObject(Constant.REDIS_PRE.EQUIPMENT_TYPE + equipmentType.getId(), equipmentType.getTypeName());
    }

    /**
     * 缓存模型数据
     */
    private void modelLoad() {
        List<Model> models = modelMapper.selectList(new QueryWrapper<>());
        for (Model model : models)
            redisCache.setCacheObject(Constant.REDIS_PRE.MODEL + model.getId(),
                    JSON.toJSONString(ModelVo.builder()
                            .id(model.getId())
                            .modelCode(model.getModelCode())
                            .modelName(model.getModelName())
                            .needleNumber(model.getNeedleNumber())
                            .needleCode(model.getNeedleCode())
                            .stockingsCode(model.getStockingsCode())
                            .build()));
    }

    /**
     * 员工信息缓存
     */
    public void operateLoad() {
        // 获取未删除的员工信息
        operatorMapper.selectOne(new QueryWrapper<Operator>().eq("id","7F0B2B16-3C0E-432B-BE66-3AA8AAD07AFF").eq("IsDeleted", 0));
        List<Operator> operators = operatorMapper.selectList(new QueryWrapper<Operator>().eq("IsDeleted", 0));
        for (Operator operator : operators)
            redisCache.setCacheObject(Constant.REDIS_PRE.OPERATE + operator.getId(),
                    JSON.toJSONString(OperatorVo.builder()
                            .id(operator.getId())
                            .realName(operator.getRealName())
                            .username(operator.getUsername())
                            .userNo(operator.getUserNo())
                            .departmentGuid(operator.getDepartmentGuid())
                            .build()));
    }

    /**
     * 部门缓存
     */
    private void departmentLoad() {
        List<Department> departments = departmentMapper.selectList(new QueryWrapper<Department>().eq("IsDeleted", 0));
        for (Department department : departments)
            redisCache.setCacheObject(Constant.REDIS_PRE.DEPARTMENT + department.getId(), department.getTitle());
    }

    /**
     * 工位信息缓存
     */
    private void stationLoad() {
        List<Station> stations = stationMapper.selectList(new QueryWrapper<>());
        for (Station station : stations)
            redisCache.setCacheObject(Constant.REDIS_PRE.STATION + station.getId(),
                    JSON.toJSONString(StationVo.builder()
                            .id(station.getId())
                            .stationCode(station.getStationCode())
                            .stationName(station.getStationName())
                            .build()));
    }

    /**
     * 设备信息缓存
     */
    private void equipmentLoad() {
        List<Equipment> equipments = equipmentMapper.selectList(new QueryWrapper<>());
        for (Equipment equipment : equipments) {
            redisCache.setCacheObject(Constant.REDIS_PRE.EQUIPMENT + equipment.getId(),
                    JSON.toJSONString(EquipmentVo.builder()
                            .id(equipment.getId())
                            .equipmentName(equipment.getEquipmentName())
                            .equipmentTypeId(equipment.getEquipmentTypeId())
                            .modelId(equipment.getModelId())
                            .needleCode(equipment.getNeedleCode())
                            .needleNumber(equipment.getNeedleNumber())
                            .stockingsCode(equipment.getStockingsCode())
                            .outreachDevice(equipment.getOutreachDevice())
                            .outreachType(equipment.getOutreachType())
                            .build()));
        }
    }

    /**
     * 工位绑定设备信息缓存
     */
    private void bindInfoLoad() {
        List<BindInfo> bindInfos = bindInfoMapper.selectList(new QueryWrapper<>());
        for (BindInfo bindInfo : bindInfos)
            redisCache.setCacheObject(Constant.REDIS_PRE.BIND_STATION_EQUIPMENT + bindInfo.getStationId(), bindInfo.getEquipmentId());
    }

}
